/**
 *    Copyright 2009-2015 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.logging;

import java.lang.reflect.Constructor;

/**
 * @author Clinton Begin
 * @author Eduardo Macarron
 */
public final class LogFactory {// Mybatis支持多个第三方日志插件，优先级由高到低为slf4j、commonsLogging、Log4J2、Log4J、JdkLog和NoLogging等。
//  除静态方法外的所有方法都是静态的
  /**
   * Marker to be used by logging implementations that support markers
   */
  public static final String MARKER = "MYBATIS";// 标签

  private static Constructor<? extends Log> logConstructor;//Log接口的实现类的构造方法

  static {// 初始化的时候会选存在的优先级最高的第三方日志插件作为日志模块的实现
    tryImplementation(new Runnable() {
      @Override
      public void run() {
        useSlf4jLogging();
      }
    });
    tryImplementation(new Runnable() {
      @Override
      public void run() {
        useCommonsLogging();
      }
    });
    tryImplementation(new Runnable() {
      @Override
      public void run() {
        useLog4J2Logging();
      }
    });
    tryImplementation(new Runnable() {
      @Override
      public void run() {
        useLog4JLogging();
      }
    });
    tryImplementation(new Runnable() {
      @Override
      public void run() {
        useJdkLogging();
      }
    });
    tryImplementation(new Runnable() {
      @Override
      public void run() {
        useNoLogging();
      }
    });
  }

  private LogFactory() {
    // disable construction
  }

  public static Log getLog(Class<?> aClass) {//实例化Log并返回Log
    return getLog(aClass.getName());
  }

  public static Log getLog(String logger) {
    try {
      return logConstructor.newInstance(new Object[] { logger });
    } catch (Throwable t) {
      throw new LogException("Error creating logger for logger " + logger + ".  Cause: " + t, t);
    }
  }

  public static synchronized void useCustomLogging(Class<? extends Log> clazz) {// configuration中会按照配置设置日志的实现
    setImplementation(clazz);
  }

  public static synchronized void useSlf4jLogging() {//同步
    setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);
  }

  public static synchronized void useCommonsLogging() {//同步
    setImplementation(org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class);
  }

  public static synchronized void useLog4JLogging() {//同步
    setImplementation(org.apache.ibatis.logging.log4j.Log4jImpl.class);
  }

  public static synchronized void useLog4J2Logging() {//同步
    setImplementation(org.apache.ibatis.logging.log4j2.Log4j2Impl.class);
  }

  public static synchronized void useJdkLogging() {//同步
    setImplementation(org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class);
  }

  public static synchronized void useStdOutLogging() {//同步
    setImplementation(org.apache.ibatis.logging.stdout.StdOutImpl.class);
  }

  public static synchronized void useNoLogging() {//同步
    setImplementation(org.apache.ibatis.logging.nologging.NoLoggingImpl.class);
  }

  //这个方法有点迷惑性，因为它使用 Runnable 接口作为参数，而 useXxxLogging() 方法又是同步方法，很容易联想到多线程，实际上这里并没有， Runnable 接口不结合 Thread 类使用它就是一个普通的函数接口。除去这些就没什么了，不过是调用了 Runnable 的 run() 方法而已。
  private static void tryImplementation(Runnable runnable) {// 共同方法
    if (logConstructor == null) {
      try {
        runnable.run();
      } catch (Throwable t) {
        // ignore
      }
    }
  }
  // 共同方法
  private static void setImplementation(Class<? extends Log> implClass) {// 日志初始化使用相应的适配器
    try {
      Constructor<? extends Log> candidate = implClass.getConstructor(new Class[] { String.class });// Log的实现类反射匹配构造函数
      Log log = candidate.newInstance(new Object[] { LogFactory.class.getName() });// 根据构造函数实例化对象
      if (log.isDebugEnabled()) {
        log.debug("Logging initialized using '" + implClass + "' adapter.");// 打印日志
      }
      logConstructor = candidate;//将候选者赋给logConstructor
    } catch (Throwable t) {
      throw new LogException("Error setting Log implementation.  Cause: " + t, t);
    }
  }

}
